#include "GlobalParam.h"
#include "TinyXML/tinyxml.h"
#include "ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

GlobalParam::GlobalParam()
{

}

void GlobalParam::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QList<paramstruct> paramstructList;
    Analysis(filePath, paramstructList);

    //添加第一个表
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    paramstruct _paramstructtemp;
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_paramstructtemp.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_paramstructtemp.toFieldTypes());

    for (int j = 0; j < paramstructList.size(); j++)
    {
        int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
        _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(paramstructList.at(j).name));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).val)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).unit)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).des)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 4, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).edit)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 5, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).type)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 6, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).uplimit)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 7, new QTableWidgetItem(QString("%1").arg(paramstructList.at(j).lowlimit)));
    }
    m_pTableWidgetFormMap.insert(0, QList<TableWidgetForm*>() << _TableWidgetForm);
}

bool GlobalParam::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.size() > 0 && m_pTableWidgetFormMap.value(0).size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            QList<paramstruct> _paramstructs;
            for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                paramstruct _paramstruct;
                _paramstruct.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                _paramstruct.val = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                _paramstruct.unit = _TableWidgetForm->getTableWidget()->item(k,2)->text();
                _paramstruct.des = _TableWidgetForm->getTableWidget()->item(k,3)->text();
                _paramstruct.edit = _TableWidgetForm->getTableWidget()->item(k,4)->text().toInt();
                _paramstruct.type = _TableWidgetForm->getTableWidget()->item(k,5)->text();
                _paramstruct.uplimit = _TableWidgetForm->getTableWidget()->item(k,6)->text().toInt();
                _paramstruct.lowlimit = _TableWidgetForm->getTableWidget()->item(k,7)->text().toInt();
                _paramstructs.append(_paramstruct);
            }
            SaveXml(filePath, _paramstructs);
            return true;
        }
    }
    return false;
}

void GlobalParam::Analysis(const QString &path, QList<paramstruct> &paramstructList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("params");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        for(TiXmlElement* childElement = rootElement->FirstChildElement("param");
            childElement != nullptr; childElement = childElement->NextSiblingElement("param"))
        {
            paramstruct _paramstruct;
            _paramstruct.name = childElement->Attribute("name");
            childElement->QueryIntAttribute("val", &_paramstruct.val);
            _paramstruct.unit = childElement->Attribute("unit");
            _paramstruct.des = childElement->Attribute("des");
            childElement->QueryIntAttribute("edit", &_paramstruct.edit);
            _paramstruct.type = childElement->Attribute("type");
            childElement->QueryIntAttribute("uplimit", &_paramstruct.uplimit);
            childElement->QueryIntAttribute("lowlimit", &_paramstruct.lowlimit);
            paramstructList.append(_paramstruct);
        }
    }
    delete xmlDocument;
}

bool GlobalParam::SaveXml(const QString &path, const QList<paramstruct> &paramstructList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("params");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        for(TiXmlElement* childElement = rootElement->FirstChildElement("param");
            childElement != nullptr; childElement = childElement->NextSiblingElement("param"))
        {
            rootElement->RemoveChild(childElement);
        }
        for (auto _paramstruct : paramstructList)
        {
            //元素类
            TiXmlElement* ChildxmlElement = new TiXmlElement("param");
            ChildxmlElement->SetAttribute("name", _paramstruct.name.toStdString().c_str());
            ChildxmlElement->SetAttribute("val", _paramstruct.val);
            ChildxmlElement->SetAttribute("unit", _paramstruct.unit.toStdString().c_str());
            ChildxmlElement->SetAttribute("des", _paramstruct.des.toStdString().c_str());
            ChildxmlElement->SetAttribute("edit", _paramstruct.edit);
            ChildxmlElement->SetAttribute("type", _paramstruct.type.toStdString().c_str());
            ChildxmlElement->SetAttribute("uplimit", _paramstruct.uplimit);
            ChildxmlElement->SetAttribute("lowlimit", _paramstruct.lowlimit);
            rootElement->LinkEndChild(ChildxmlElement);
        }
    }

    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
